package com.sunlands.zlcx.usercenter.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sunlands.zlcx.usercenter.constant.ResultStatus;
import com.sunlands.zlcx.usercenter.util.AsyncTaskManager;
import com.sunlands.zlcx.usercenter.util.HttpUtil;
import com.sunlands.zlcx.usercenter.vo.response.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.HashMap;
import java.util.Map;

@Slf4j
public class BaseController {

    @Autowired
    AsyncTaskManager asyncTaskManager;
    @Autowired
    private RedisTemplate redisTemplate;

    @Value("${shangde.words.filter.url}")
    private String wordsFilterUrl;

    public static Object[] arg = new Object[]{};

    public ResultVO getPramaErrorJson(String message, String alertMessage) {
        ResultVO resultVO = new ResultVO();
        resultVO.setMessage(message);
        resultVO.setAlertMessage(alertMessage);
        return resultVO;
    }

    public ResultVO failState(String message, Object content) {

        ResultVO resultVO = new ResultVO();
        resultVO.setState(ResultStatus.FAILED.getCode());
        resultVO.setMessage(message);
        resultVO.setAlertMessage(message);
        resultVO.setContent(content);

        return resultVO;
    }

    public  ResultVO successState(String message,Object content) {

        ResultVO resultVO = new ResultVO();
        resultVO.setState(ResultStatus.SUCCESS.getCode());
        resultVO.setMessage(message);
        resultVO.setAlertMessage(message);
        resultVO.setContent(content);

        return resultVO;
    }

    /**
     * 敏感词校验
     * 如果包含敏感词则返回true
     * @param word
     * @return
     */
    public boolean checkSensitiveWord(String word){
        Map<String, Object> param = new HashMap<>();
        param.put("word" , word);
        try {
            String result = HttpUtil.doPost(wordsFilterUrl, param);
            JSONObject jsonObject = JSON.parseObject(result);
            if (jsonObject.getBoolean("content") ){
                return true ;
            }
        } catch (Exception e) {
            log.error("敏感词校验异常，url={},param={},e={}",wordsFilterUrl, JSON.toJSONString(param),e);
        }
        return false;
    }



}
